package com.yuwei.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.tencent.mmkv.MMKV
import com.yuwei.base.RoutePath

/**
 * token拦截器
*/
@Interceptor(priority = 4, name = "路由Token拦截器")
class RouteTokenInterceptor : IInterceptor{
    private lateinit var mContext: Context
    override fun init(context: Context?) {
        if(context!=null){
            mContext = context
        }
    }

    override fun process(postcard: Postcard?, callback: InterceptorCallback?) {
        val loginToken = MMKV.defaultMMKV().decodeString("token")
        if(loginToken.isNullOrEmpty()&& postcard?.path!= RoutePath.LOGIN_ACTIVITY
            && postcard?.path != RoutePath.MODULE_CODE_LOGIN_ACTIVITY
            && postcard?.path != RoutePath.MODULE_LOGIN_HELP_ACTIVITY
            && postcard?.path != RoutePath.MODULE_USER_LOGIN_ACTIVITY
            && postcard?.path != RoutePath.MODULE_LOGIN_WEB_ACTIVITY
            && postcard?.path != RoutePath.MODULE_PHONE_LOGIN_ACTIVITY
            && postcard?.path != RoutePath.MODULE_LOGIN_ALL_HELP_ACTIVITY
            && postcard?.path != RoutePath.MODULE_NATIVE_LOGIN_ACTIVITY
            && postcard?.path != RoutePath.MODULE_LOGIN_ACTIVITY){
            ToastUtils.showLong("请先登录")
            ARouter.getInstance().build(RoutePath.LOGIN_ACTIVITY).navigation()
        }else{
            callback?.onContinue(postcard)
        }
    }
}